package com.turing.post.report.impl;

import java.util.Date;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.AccSysCardDaySettDao;

@Stateless(mappedName = "report.impl.AccSysCardDaySettDaoImpl")
@Remote
@Local
public class AccSysCardDaySettDaoImpl implements AccSysCardDaySettDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	public void generateAccSysCardDaySett(String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery("insert into AccSysCardDaySett ( IssueCompanyNo, TranDate, CardClassicId, "
						+ "LastBalance, SaleAmount, DepositAmount, ConsumeAmount, RedemingAmount, FeeAmount, AdjustAmount, Balance ) "
						+ "select t.issueCompanyNo, t.tranDate, t.cardClassicId, "
						+ "sum(t.lastBalance), sum(t.saleAmount), sum(t.depositAmount), sum(t.consumeAmount), sum(t.redemingAmount), sum(t.feeAmount), sum(t.adjustAmount), sum(t.balance) "
						+ "from AccSysBranchCardDaySett t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2 "
						+ "group by t.issueCompanyNo, t.tranDate, t.cardClassicId");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}

	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeAllAccSysCardDaySettByTranDate(String issueCompanyNo,
			Date tranDate) {
		Query query = _em.createNativeQuery("delete from AccSysCardDaySett t "
				+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}
}
